package com.ahaha.homeworkday2

import java.io.{File, PrintWriter}

import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable
import scala.io.Source

object UserTest {
    def main(args: Array[String]): Unit = {
        val conf = new SparkConf()
            .setAppName(s"${CountTest.getClass.getSimpleName}")
            .setMaster("local[*]")
        val sc = new SparkContext(conf)
        UserTest(sc)
        sc.stop()

    }

    def UserTest(sc:SparkContext): Unit ={
        case class FriendRel(friend:String,user:String){
            override def hashCode(): Int = super.hashCode()

            override def equals(obj: Any): Boolean = super.equals(obj)
        }
        val source =  Source.fromFile("data/friendinfo.txt")
        val list = source.getLines().toArray
        val array = sc.parallelize(list)
        for (elem <- array) {
            println(elem)
        }
        val rels = array.map(t => {
            val strings = t.split("\\s+")
            FriendRel(strings(1), strings(0))
        })
        val stringToRel = mutable.Map[String, FriendRel]()
        val value = sc.broadcast(stringToRel)
        for(e <- rels){
            value.value.put(e.friend,e)
        }
        val map = value.value
        println("不同id的个数为：" + (map.size+1))
        /*for (elem <- value.value) {{
            println(elem)
        }}*/

        println(s"不同id对的个数为：${map.size}")
       /* for (elem <- map) {
            println(e)
        }*/
        val stringToStringToRel = map.groupBy(_._1)
        for (elem <- stringToStringToRel) {
            println(s"${elem._1}的粉丝的个数为：${elem._2.size}")
        }
        val writer = new PrintWriter(new File("data/friends.txt"))
        for (elem <- stringToStringToRel) {
            writer.write(s"${elem._1}的粉丝的个数为：${elem._2.size}\n")
            println
        }
        writer.close()





    }
}
